import java.util.Comparator;
import java.util.PriorityQueue;

public class Solution {
    public int[] smallestK(int[] arr, int k) {
        if(arr == null || k<1){
            return new int[0];
        }
        int[] tmp = new int[k];
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<Integer>(k,new Comparator<Integer>() {

            @Override
            public int compare(Integer o1, Integer o2) {
                return o2.compareTo(o1);
            }
        }
        );
        for (int i = 0; i < k; i++) {
            priorityQueue.offer(arr[i]);
        }

        for (int i = k; i < arr.length; i++) {
            if(priorityQueue.peek() > arr[i]){
                priorityQueue.poll();
                priorityQueue.offer(arr[i]);
            }
        }
        for (int i = 0; i < k; i++) {
            tmp[i] = priorityQueue.poll();
        }
        return tmp;
    }
}
